package org.hu.service;

import org.hu.mapper.MeetingMapper;
import org.hu.model.Meeting;
import org.hu.model.MeetingDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MeetingService {
    @Autowired
    MeetingMapper meetingMapper;


    /**
     * 添加会议
     * @param meeting 会议对象
     * @param mps   参加员工数组
     * @return 1为成功
     */
    public Integer addMeeting(Meeting meeting, Integer[] mps) {
        meeting.setReservationtime(new Date());
        meeting.setStatus(0);
        Integer result = meetingMapper.addMeeting(meeting);
        meetingMapper.addParticipants(meeting.getMeetingid(), mps);
        return result;
    }

    /**
     * 查询会议并分页
     * @param meetingDTO 会议
     * @param page 页数
     * @param pagesize 每页数量
     * @return List<MeetingDTO>
     */
    public List<MeetingDTO> listMeetingDTOs(MeetingDTO meetingDTO, Integer page, Integer pagesize) {
        page = (page - 1) * pagesize;
        return meetingMapper.listMeetingDTOs(meetingDTO, page, pagesize);
    }

    /**
     * 获得所有会议的数量
     * @param meeting 会议
     * @return Long
     */
    public Long getTotal(MeetingDTO meeting) {
    return meetingMapper.getTotal(meeting);}

    /**
     * 通过会议id查询会议
     * @param meetingid
     * @return Meeting
     */
    public Meeting getMtById(Integer meetingid) {
        return meetingMapper.getMtById(meetingid);
    }

    /**
     * 我预定的会议
     * @param employeeid 员工id
     * @return
     */
    public List<MeetingDTO> getMyBookMt(Integer employeeid) {
        return meetingMapper.getMyBookMt(employeeid);
    }

    /**
     * 通过会议id取消会议
     * @param meetingid 会议id
     * @param reason 取消原因
     * @return Integer
     */
    public Integer cancelMeeting(Integer meetingid, String reason) {
        return meetingMapper.cancelMeeting(meetingid,reason);
    }

    /**
     * 根据会议状态查询会议
     * @param status 状态码 0为正常 1为取消
     * @return
     */
    public List<MeetingDTO> getMtByStatus(int status) {
        return meetingMapper.getMtByStatus(status);
    }

    /**
     * 获取员工的最近七日会议
     * @param employeeid 员工id
     * @return List<MeetingDTO>
     */
    public List<MeetingDTO> getSevenDaysMt(Integer employeeid) {
        List<MeetingDTO> list = meetingMapper.getMtByEmpId(employeeid);

        Calendar c = Calendar.getInstance();
        Date now = new Date();
        c.setTime(now);
        c.add(Calendar.DATE, 7);
        Date after7day = c.getTime();

        //得到未来7天内需要此员工参加的会议
        List<MeetingDTO> listNext7Day =
                list.stream().filter(s -> s.getStarttime().getTime() >= now.getTime() &&
                                          s.getStarttime().getTime() <= after7day.getTime())
                        .collect(Collectors.toList());
        return listNext7Day;
    }

    /**
     * 通过员工id查询员工参加的全部会议
     * @param employeeid 员工id
     * @return List<MeetingDTO>
     */
    public List<MeetingDTO> getMtByEmpId(Integer employeeid) {
        return meetingMapper.getMtByEmpId(employeeid);
    }
}
